package com.party.admin.biz.mall;

import com.google.common.base.Function;
import com.party.admin.biz.file.FileBizService;
import com.party.admin.web.dto.output.mall.MallBrandOutput;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.mall.MallBrand;
import com.party.core.service.mall.IMallBrandService;
import com.party.core.service.cos.CosBizService;
import com.party.core.utils.MyBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 品牌业务处理
 * Created by yifeng
 *
 * @date 2018/4/10 0010
 * @time 上午 10:24
 */
@Service
public class MallBrandBizService {
    @Autowired
    private IMallBrandService mallBrandService;
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private CosBizService cosBizService;

    @Value("#{party['shop.url.protocol']}")
    private String shopUrl;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 更新商品数
     *
     * @param oldBrandId 旧的品牌id
     * @param newBrandId 新的品牌id
     */
    public void updateGoodsNum(String oldBrandId, String newBrandId) {
        if (!oldBrandId.equals(newBrandId)) {
            if (StringUtils.isNotEmpty(oldBrandId)) {
                MallBrand oldGoodsBrand = mallBrandService.get(oldBrandId);
                oldGoodsBrand.setGoodsNum(oldGoodsBrand.getGoodsNum() - 1);
                mallBrandService.update(oldGoodsBrand);
            }
            if (StringUtils.isNotEmpty(newBrandId)) {
                MallBrand goodsBrand = mallBrandService.get(newBrandId);
                goodsBrand.setGoodsNum(goodsBrand.getGoodsNum() + 1);
                mallBrandService.update(goodsBrand);
            }
        }
    }

    /**
     * 品牌商品数减一
     *
     * @param brandId
     */
    public void subGoodsNum(String brandId) {
        MallBrand mallBrand = mallBrandService.get(brandId);
        mallBrand.setGoodsNum(mallBrand.getGoodsNum() - 1);
        mallBrandService.update(mallBrand);
    }

    /**
     * 品牌列表
     *
     * @param goodsBrand 品牌
     * @param params     查询条件
     * @param page       分页参数
     * @return
     */
    public List<MallBrandOutput> brandList(MallBrand goodsBrand, Map<String, Object> params, Page page) {
        List<MallBrand> list = mallBrandService.webListPage(goodsBrand, params, page);
        List<MallBrandOutput> brandOutputs = LangUtils.transform(list, new Function<MallBrand, MallBrandOutput>() {
            @Override
            public MallBrandOutput apply(MallBrand input) {
                MallBrandOutput output = new MallBrandOutput();
                try {
                    MyBeanUtils.copyBeanNotNull2Bean(input, output);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String filePath = fileBizService.getUploadCIPath(input.getId(), "mallBrand");
                String qrCodeUrl = cosBizService.getPreviewPictureUrl(filePath + input.getId());
                output.setQrCodeUrl(qrCodeUrl);
                return output;
            }
        });
        return brandOutputs;
    }

    /**
     * 保存品牌
     *
     * @param mallBrand 品牌
     */
    public void saveBrand(MallBrand mallBrand) {
        if (StringUtils.isEmpty(mallBrand.getId())) {
            mallBrandService.insert(mallBrand);

            try {
                String filePath = fileBizService.getUploadCIPath(mallBrand.getId(), "mallBrand");
                String content = shopUrl + "brand/" + mallBrand.getId();
                fileBizService.uploadCloudOut(mallBrand.getId(), filePath, content, null, false);
            } catch (Exception e) {
                logger.error("上传品牌二维码异常", e);
            }
        } else {
            try {
                String filePath = fileBizService.getUploadCIPath(mallBrand.getId(), "mallBrand");
                String content = shopUrl + "brand/" + mallBrand.getId();
                fileBizService.uploadCloudOut(mallBrand.getId(), filePath, content, null, false);
            } catch (Exception e) {
                logger.error("上传品牌二维码异常", e);
            }

            MallBrand brandDb = mallBrandService.get(mallBrand.getId());
            if (brandDb.getGoodsNum() > 0) {
                throw new BusinessException("该品牌已分配，不能修改");
            }
            try {
                MyBeanUtils.copyBeanNotNull2Bean(mallBrand, brandDb);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mallBrandService.update(brandDb);
        }
    }

    /**
     * 初始化二维码
     *
     * @param brandId 品牌id
     */
    public void initQrCode(String brandId) {
        try {
            String filePath = fileBizService.getUploadCIPath(brandId, "mallBrand");
            String content = shopUrl + "brand/" + brandId;
            fileBizService.uploadCloudOut(brandId, filePath, content, null, false);
        } catch (Exception e) {
            logger.error("上传品牌二维码异常", e);
        }
    }
}
